#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QLabel>
#include<QProgressBar>
#include<QSpinBox>
#include<QFontComboBox>
#include<QActionGroup>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    buildUI();
    buildSignalSlots();

    //ui->toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);

    this->setCentralWidget(ui->textEdit);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::buildUI()
{
    labFile = new QLabel(this);
    labFile->setMinimumWidth(150);
    labFile->setText("文件名：");
    ui->statusbar->addWidget(labFile);

    progressBar1 = new QProgressBar(this);
    progressBar1->setMaximumWidth(200);
    progressBar1->setMinimum(5);
    progressBar1->setMaximum(50);
    progressBar1->setValue(ui->textEdit->font().pointSize());
    ui->statusbar->addWidget(progressBar1);

    labInfo = new QLabel(this);
    labInfo->setText("选择字体名称：");
    ui->statusbar->addPermanentWidget(labInfo);

    actionGroup = new QActionGroup(this);
    actionGroup->addAction(ui->actLang_CN);
    actionGroup->addAction(ui->actLang_EN);
    actionGroup->setExclusive(true);
    ui->actLang_CN->setChecked(true);

    spinFontSize = new QSpinBox(this);
    spinFontSize->setMinimum(5);
    spinFontSize->setMaximum(50);
    spinFontSize->setValue(ui->textEdit->font().pointSize());
    spinFontSize->setMinimumWidth(50);
    ui->toolBar->addWidget(spinFontSize);

    comboFontName = new QFontComboBox(this);
    comboFontName->setMinimumWidth(150);
    ui->toolBar->addWidget(comboFontName);
    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ui->actClose);

}

void MainWindow::buildSignalSlots()
{
    connect(spinFontSize, SIGNAL(valueChanged(int)), this, SLOT(do_fontSize_changed(int)));
    connect(comboFontName, &QFontComboBox::currentFontChanged, this, &MainWindow::do_fontSelected);

    connect(ui->actFont_Bold, SIGNAL(triggered(bool)), this, SLOT(on_actFont_Bold_triggered(bool)));
    connect(ui->actFont_Italic, SIGNAL(triggered(bool)), this, SLOT(on_actFont_Italic_triggered(bool)));
    connect(ui->actFont_UnderLine, SIGNAL(triggered(bool)), this, SLOT(on_actFont_UnderLine_triggered(bool)));

    connect(ui->actFile_Save, SIGNAL(triggered()), this, SLOT(on_actFile_Save_triggered()));
    connect(ui->actSys_ToggleText, SIGNAL(triggered(bool)), this, SLOT(on_actSys_ToggleText_triggered(bool)));

}

void MainWindow::do_fontSize_changed(int fontSize)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontPointSize(fontSize);
    ui->textEdit->mergeCurrentCharFormat(fmt);
    progressBar1->setValue(fontSize);

}

void MainWindow::do_fontSelected(const QFont &font)
{
    labInfo->setText("字体名称：" + font.family());
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFont(font);
    ui->textEdit->mergeCurrentCharFormat(fmt);

}

void MainWindow::on_actFont_Bold_triggered(bool checked)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();

    if(checked)
    {
        fmt.setFontWeight(QFont::Bold);
    }
    else
    {
        fmt.setFontWeight(QFont::Normal);
    }

    ui->textEdit->mergeCurrentCharFormat(fmt);

}

void MainWindow::on_actFont_Italic_triggered(bool checked)
{

    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontItalic(checked);
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_actFont_UnderLine_triggered(bool checked)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontUnderline(checked);
    ui->textEdit->mergeCurrentCharFormat(fmt);

}

void MainWindow::on_actFile_Save_triggered()
{
    ui->textEdit->document()->setModified(false);
    labFile->setText("文件已保存");

}

void MainWindow::on_actSys_ToggleText_triggered(bool checked)
{
    if(checked)
    {
        ui->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    }
    else
    {
        ui->toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
    }

}

void MainWindow::on_textEdit_copyAvailable(bool b)
{
    ui->actEdit_Cut->setEnabled(b);
    ui->actEdit_Copy->setEnabled(b);
    ui->actEdit_Paste->setEnabled(ui->textEdit->canPaste());

}

void MainWindow::on_textEdit_selectionChanged()
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    ui->actFont_Bold->setChecked(fmt.font().bold());
    ui->actFont_Italic->setChecked(fmt.fontItalic());
    ui->actFont_UnderLine->setChecked(fmt.fontUnderline());

}

